﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebsiteAdmin.Wilson
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDBData();
            }

        }
        protected DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("fileid", typeof(Int32));
            dt.Columns.Add("filegroup", typeof(string));
            dt.Columns.Add("size", typeof(string));
            dt.Columns.Add("maxsize", typeof(string));
            dt.Columns.Add("growth", typeof(string));
            dt.Columns.Add("usage", typeof(string));
            dt.Columns.Add("filename", typeof(string));
            return dt;
        }

        public void GetDBData()
        {
            using (DataTable dt = Bll.Setting.GETDBSIZE(Global.ConnWilson))
            {
                DataTable dtRtn = CreateDataTable();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow newRow = dtRtn.NewRow();
                        newRow["name"] = dt.Rows[i]["name"].ToString();
                        newRow["fileid"] = Convert.ToInt32(dt.Rows[i]["fileid"]);
                        newRow["filegroup"] = dt.Rows[i]["filegroup"].ToString();
                        newRow["size"] = (Convert.ToInt64(dt.Rows[i]["size"].ToString().Replace(" KB", "")) / 1024).ToString() + " MB";

                        if (dt.Rows[i]["maxsize"].ToString() == "Unlimited" || dt.Rows[i]["maxsize"].ToString() == "2147483648 KB")
                            newRow["maxsize"] = "Unlimited";
                        else
                            newRow["maxsize"] = (Convert.ToInt64(dt.Rows[i]["maxsize"].ToString().Replace(" KB", "")) / 1024).ToString() + " MB";
                        newRow["growth"] = dt.Rows[i]["growth"].ToString();
                        newRow["usage"] = dt.Rows[i]["usage"].ToString();
                        newRow["filename"] = dt.Rows[i]["filename"].ToString();
                        dtRtn.Rows.Add(newRow);
                    }
                }
                if (dtRtn.Rows.Count > 0)
                {
                    RP_Db.DataSource = dtRtn.DefaultView;
                    RP_Db.DataBind();
                    dtRtn.Dispose();
                }
            }
        }

        protected void DeleteLog_Click(object sender, EventArgs e)
        {
            int rtn = Bll.Setting.DeleteLog(Global.ConnWilson);
            Response.Redirect("/Wilson/Dashboard.aspx");
        }

    }
}
